package wms.util;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {
	
	private Integer totalCount;  //总记录数
	private Integer pageCount=0; //总页数
	private Integer pageSize;	 //每页大小
	private Integer page;		 //当前页
	private Integer num;	     //当前页前后显示的页数
	private List<?> list = new ArrayList<>();
	
	public PageUtil(Integer currIndex, Integer pageSize) {
		this.page = currIndex;
		this.pageSize = pageSize;
		this.num = 3;
	}
	
	public Integer getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Integer totalCount) {
		if (totalCount > 0) {
			this.totalCount = totalCount;
			if (this.totalCount % pageSize == 0)
				this.pageCount = this.totalCount / pageSize;
			else if (this.totalCount % pageSize > 0)
				this.pageCount =  (this.totalCount / pageSize )+ 1;
			else
				this.pageCount = 0;
		}
	}
	public Integer getPageCount() {
		return pageCount;
	}
	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	/**
	 * 前后页面的个数
	 * @return
	 */
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	
	/**
	 * 前一页
	 * @return
	 */
	public Integer getPrev() {
		Integer prev = page - 1;
		return prev < 0 ? page : prev;
	}
	
	
	/**
	 * 后一页
	 * @return
	 */
	public Integer getNext() {
		Integer next = page + 1;
		return next > pageCount ? pageCount : next;
	}
	
	/**
	 * 最后一页
	 * @return
	 */
	public Integer getLast(){
		return pageCount;
	}
	
	public boolean isPrev(){
		return page > 1;
	}
	
	public boolean isNext(){
		return (pageCount != null && page < pageCount);
	}
	
	public List<Integer> getPrevNumPages() {
		List<Integer> list = new ArrayList<>();
		//前num的其实  其实  默认为1
		Integer frontStart = 1; 
		//如果当前页数 大于 规定的num时 才会触发
		if (page > num) {
			frontStart = page - num;
		}
		for (Integer i = frontStart; i < page; i++) {
			list.add(i);
		}
		return list;
	}
	
	public List<Integer> getNextNumPages() {
		List<Integer> list = new ArrayList<>();
		//后num的页数的截至位置   默认就是num数量 比如从第一页开始
		Integer backEnd = num; 
		if (pageCount != null) {
			if(num < pageCount && (page + num) < pageCount) {
				backEnd =  backEnd + page;
			} else 
				backEnd = pageCount;
			
			for (Integer i = page +1 ; i <= backEnd; i++) {
				list.add(i);
			}
		}
		
		
		return list;
	}
}
